import { shallowRef } from 'vue'

import { back, catchFn } from '@/libs/common'
import { getHistoryResult } from './api'
import { update as updateResult } from '../result/data'

/** MBTI测试结果 */
const list = shallowRef<T_MBTIResultHistory[]>([])

export { list }

/**
 * 更新历史测试列表
 */
const update = () =>
  catchFn(async () => {
    const { data } = await getHistoryResult()

    list.value = data
  })

/**
 * 选择历史记录查看
 * @param item 历史记录
 */
const select = async (item: T_MBTIResultHistory) => {
  const res = await updateResult(item.typeCode, item.sessionId)

  res && back('/pages/quiz/mbti/result/index')
}

/**
 * 还原状态
 */
const clear = () => (list.value = [])

export { update, select, clear }
